package com.tianquan.Thread;/**
 * @author starsea
 * @date 2024-10-14 上午9:35
 */

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @BelongsProject: uploaddemo
 * @BelongsPackage: com.tianquan.Thread
 * @Author: tianquan
 * @CreateTime: 2024-10-14  09:35
 * @Description: 测试
 * @Version: 1.0
 */
public class MyTread {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        AtomicInteger number = new AtomicInteger(1);
        Object lock = new Object();

        Thread evenThread = new Thread(() -> {
            while (number.get() <= 100) {
                synchronized (lock) {
                    if (number.get() % 2 == 0) {
                        System.out.println("Even Thread: " + number.getAndIncrement());
                        lock.notify();
                        if (number.get() <= 100) {
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                Thread.currentThread().interrupt();
                            }
                        }
                    }
                }
            }
        });

        Thread oddThread = new Thread(() -> {
            while (number.get() <= 100) {
                synchronized (lock) {
                    if (number.get() % 2 == 1) {
                        System.out.println("Odd Thread: " + number.getAndIncrement());
                        lock.notify();
                        if (number.get() <= 100) {
                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                Thread.currentThread().interrupt();
                            }
                        }
                    }
                }
            }
        });

        evenThread.start();
        oddThread.start();


        // MyTask task = new MyTask();
        // FutureTask<String> futureTask = new FutureTask(task);
        // new Thread(futureTask).start();
        // System.out.println(futureTask.get());
    }

   static class MyTask implements Callable<String> {

        @Override
        public String call() throws Exception {
            return "tianquan";
        }
    }




}
